<?php
/**
* User: zhyu
 * Date: 2017年3月28日
 *描述
 *
 *　　　　　　　　┏┓　　　┏┓+ +
 *　　　　　　　┏┛┻━━━┛┻┓ + +
 *　　　　　　　┃　　　　　　　┃ 　
 *　　　　　　　┃　　　━　　　┃ ++ + + +
 *　　　　　　 ████━████ ┃+
 *　　　　　　　┃　　　　　　　┃ +
 *　　　　　　　┃　　　┻　　　┃
 *　　　　　　　┃　　　　　　　┃ + +
 *　　　　　　　┗━┓　　　┏━┛
 *　　　　　　　　　┃　　　┃　　　　　　　　　　　
 *　　　　　　　　　┃　　　┃ + + + +
 *　　　　　　　　　┃　　　┃　　　　Code is far away from bug with the animal protecting　　　　　　　
 *　　　　　　　　　┃　　　┃ + 　　　　神兽保佑,代码无bug　　
 *　　　　　　　　　┃　　　┃
 *　　　　　　　　　┃　　　┃　　+　　　　　　　　　
 *　　　　　　　　　┃　 　　┗━━━┓ + +
 *　　　　　　　　　┃ 　　　　　　　┣┓
 *　　　　　　　　　┃ 　　　　　　　┏┛
 *　　　　　　　　　┗┓┓┏━┳┓┏┛ + + + +
 *　　　　　　　　　　┃┫┫　┃┫┫
 *　　　　　　　　　　┗┻┛　┗┻┛+ + + +
  *         ━━━━━━感觉萌萌哒━━━━━━
 */
class Bonus extends PC_Controller{
    protected $go_url = '/admin/bonus/user_list';
    public function __construct(){
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('area_model');
    }
    /**
     * some_func
     * 业绩统计列表
     *
     * @access public
     * @param mixed
     * @param mixed
     * @param mixed
     * @since 1.0
     * @return array
     */
    public function user_list() {
        //$this->data['username'] =  empty($_POST) ? '' : $_POST['username'];
        //         $this->data['phone'] =  empty($_POST) ? '' : $_POST['phone'];
        //         $this->data['parent_id']=  empty($_GET)?'':$_GET['id'];
        $this->data['start_time']= empty($_POST['start_time'])?'':$_POST['start_time'];
        $this->data['end_time']= empty($_POST['end_time'])?'':$_POST['end_time'];
        $this->data['message']='1';
        $this->data['authority'] = $this->authority;
        $this->load->view('/admin/bonus_list',$this->data);
    }
    
    /**
     * @brief ajxa获取会员列表数据
     * @param Null
     * @par 2015/10/12 Ver 1.00 Created by Allen
     */
    public function ajax_user_list() {
    
        $data = $_GET;
        $start = $data['iDisplayStart']; //显示的起始索引
        $length = $data['iDisplayLength'];//显示的行数
        $sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列
        $sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
        $search = $data ['sSearch'];//全局搜索字段
//      $data['username'] ? $where['user.username like'] = '%'.trim($data['username']).'%' :'';
        $where='1=1 ';
        $frist_day=$data['start_time']."-01";
        $data['start_time']?$where.=' and user.shengji_time <= '.strtotime("$frist_day +1 month -1 day"):'';
        //$data['end_time']?$where.=' and user.s_time <='.strtotime($data['end_time']).' ':'';
         
        $this->load->model('user_level_model');
        $user_level=$this->user_level_model->getUserLevelList(array('is_bonus'=>1,'status'=>1));
        $where_level=array();
        foreach($user_level as $ukey=>$uvalue){
            if(isset($uvalue['id']) && $uvalue['id']){
                $where_level[]=$uvalue['id'];
            }
        }
        if(!empty($where_level)){
            $where.=' and user.user_level in ('.implode(',', $where_level).') ';
        }
        
        $this->data['count'] = $this->user_model->getCount($where);
        //$group_performance=$this->user_model->select('sum(price) as group_performance')->find();
        $aaData = array();
        if($this->data['count']) {
            $order_by = $sort_th." ".$sort_type;
            $user_list = $this->user_model->getUserList($where,$length,$start,$order_by);
             
            $this->load->model('user_level_model');
            $user_tmp=array();
            $add_next_status=$this->Isallow('user','add_next');
            $tixian_status=$this->Isallow('cash','add_page');
            $system_money_status=$this->Isallow('system_money', 'add_page');
            $zhuan_system_status=false;
            $zhuan_system_status=$this->Isallow('system_zhuan','add_page');
    
    
            $authority = $this->authority;
            foreach($user_list as $key => $value) {
                $user_list[$key]['check'] = '<label><input name="c_id" type="checkbox" value="'.$value['id'].'" /></label>';
                // $user_list[$key]['group_performance']=$group_performance['group_performance'];
                if($value['parent_id'] ){
                    if(in_array($value['parent_id'], $user_tmp)){
                        $user_list[$key]['parent_name']=$user_tmp[$value['parent_id']]['parent_name'];
                        $user_list[$key]['parent_nick']=$user_tmp[$value['parent_id']]['user_nick'];
                    }else{
                        $user = $this->user_model->checkUser(array('user.id'=>$value['parent_id']));
                        $user_tmp[$value['parent_id']]['parent_name']=$user['username'];
                        $user_tmp[$value['parent_id']]['user_nick']=$user['user_nick'];
                        $user_list[$key]['parent_name']=$user_tmp[$value['parent_id']]['parent_name'];
                        $user_list[$key]['parent_nick']=$user_tmp[$value['parent_id']]['user_nick'];
                    }
                }else{
                    $user_list[$key]['parent_name']='无';
                    $user_list[$key]['parent_nick']='无';
                }
                if(!$value['color']){
                    $value['color']='green';
                }
                $user_list[$key]['user_level_name']='<b style="color:'.$value['color'].';">'.$value['user_level_name'].'</b>';
                $user_list[$key]['create_time']=date('Y-m-d',$value['create_time']);
                $user_list[$key]['s_time']=date('Y-m-d',$value['s_time']);
                $port = unserialize($value['areas']);
                $user_list[$key]['areas'] = $port['detail'].$value['address'];
                $user_list[$key]['avatar'] = '<img src = "'.$value['avatar'].'" style="height:30px;"/>';
                $edit_url='';
                $user_list[$key]['bonus_money'] = '<input type="text" name="bonus_money_'.$value['id'].'" id="bonus_money_'.$value['id'].'"/>';
                $user_list[$key]['remark'] = '<input type="text" user_level="'.$value['user_level_name'].'" system_money="'.$value['system_balance'].'" name="remark_'.$value['id'].'" id="remark_'.$value['id'].'"/>';
                $team_url='';
                if($this->admin['admin_type']=='admin'){
                    $team_url= '<a href="/admin/user/team_page/?id='.$value['id'].'"><span class="btn btn-xs btn-success">查看团队</span></a>';
                }
                $user_list[$key]['operate'] = $team_url;
            }
            $aaData = $user_list;
    
        }
        $output['aaData'] = $aaData;
        $output['sEcho'] = $_GET['sEcho'];
        $output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
        $output['iTotalRecords'] = $this->data['count']; //总共有几条数据
        echo json_encode($output); //最后把数据以json格式返回
    }
    //验证报单币是否足够
    public function checkMoney(){
        $result=array('flag'=>false,'errorMsg'=>'');
        if(isset($_POST['username']) && $_POST['username']){
            if(isset($_POST['bonus_money']) && $_POST['bonus_money']){
                $user=$this->user_model->checkUser(array('username'=>$_POST['username']));
                if(isset($user['system_balance'])){
                    if($user['system_balance'] >=$_POST['bonus_money']){
                    $result['flag']=true;
                    }else{
                        $result['errorMsg']='报单币余额不足';
                    }
                }else{
                        $result['errorMsg']='出账人不存在';
                    }
            }else{
                $result['errorMsg']='请输入分红金额';
            }
            
        }else{
            $result['errorMsg']='请输入出账人账户';
        }
        echo json_encode($result);
    }
    
    /** 
    * some_func  
    * 处理分红
    * 
    * @access public 
    * @param mixed 
    * @param mixed 
    * @param mixed 
    * @since 1.0 
    * @return array 
    */  
    public function doBonus() {
        
        $user=$this->user_model->checkUser(array('username'=>$_POST['chuzhang_name']));
        $bonus_log=array();
        $this->db->trans_start();
        $ids=explode(',',$_POST['checids']);
        $pici=time();
        $this->load->model('bonus_log_model');
        foreach ($ids as $key=>$value){
            if($value){
                $bonus_log=array();
                //bonus_money_316  remark_316
                $to_user_id=$value;
                $touser=$this->user_model->checkUser(array('id'=>$to_user_id));
                $bonus_money=$_POST['bonus_money_'.$to_user_id];
                $from_yue=$user['system_balance']-$bonus_money;
                $feiyong=$bonus_money*0.05;
                $shiji_money=$bonus_money*0.95;
                $yue=$touser['system_balance']+$shiji_money;
                //先处理出账人报单币
                $sql="update `user` set system_balance = system_balance - ".$bonus_money .' where id ='.$user['id'];
                $this->db->query($sql);
                //处理报单币日志
                $logdata[]=array(
                    'user_id'=>$user['id'],
                    'username'=>$user['username'],
                    'true_name'=>$user['true_name'],
                    'money'=>'-'.$bonus_money,
                    'type' =>'bonus',
                    'remark'=>$user['true_name'].'您于'.date('Y-m-d H:i:s',time())."给".$touser['username'].'分红'.$bonus_money."元.余额".$from_yue,
                    'create_time'=>  time()
                );
                $this->add_system_money_log($logdata);
                unset($logdata);
                //处理分红人的报单币金额
                $sql="update `user` set system_balance = system_balance + ".$shiji_money .' where id ='.$to_user_id;
                $this->db->query($sql);
                //处理报单币日志
                $logdata[]=array(
                    'user_id'=>$touser['id'],
                    'username'=>$touser['username'],
                    'true_name'=>$touser['true_name'],
                    'money'=>$shiji_money,
                    'type' =>'bonus',
                    'feiyong'=>$feiyong,
                    'remark'=>$touser['username'].'您于'.date('Y-m-d H:i:s',time())."收到".$user['username'].'的分红'.$bonus_money."元,扣除费用".$feiyong.'实际到账'.$shiji_money.'.余额'.$yue,
                    'bonus_remark'=>$_POST['remark_'.$value],
                    'create_time'=>  time()
                );
                $this->add_system_money_log($logdata);
                unset($logdata);
                //记录分红日志
                $bonus_log['money']         =$bonus_money;
                $bonus_log['shiji_money']   =$shiji_money;
                $bonus_log['feiyong']       = $feiyong;
                $bonus_log['pici']              =$pici;
                $bonus_log['create_time']       =time();
                $bonus_log['remark']       =$_POST['remark_'.$value];
                $bonus_log['chuzhang']      =$user['username'];
                $bonus_log['user_name'] =$touser['username'];
                $bonus_log['chuzhuang_id'] =$user['id'];
                $bonus_log['user_id'] =$touser['id'];
                $this->bonus_log_model->addBonusLog($bonus_log);
            }
        }
        $this->db->trans_complete();
        $this->location_href($this->go_url."/4");exit;
    } 
}